<template>
    <div class="chart-container">
        <div ref="chart" style="width: 100%; height: 100%"></div>
    </div>
</template>

<script>
import * as echarts from 'echarts';
import 'echarts-gl';

export default {
    name: 'HelloWorld',
    data() {
        return {
            option: {},
            dataList: [
                {
                    name: '无条件共享',
                    val: 1372,
                    itemStyle: {
                        color: 'rgba(0, 81, 180, 0.5)',
                    },
                },
                {
                    name: '有条件共享',
                    val: 979,
                    itemStyle: {
                        color: 'rgba(255, 196, 0, 0.5)',
                    },
                },
            ],
        };
    },
    mounted() {
        this.initChart();
    },
    methods: {
        getParametricEquation(startRatio, endRatio, isSelected, isHovered, k, height) {
            let midRatio = (startRatio + endRatio) / 3;
            let startRadian = startRatio * Math.PI * 2;
            let endRadian = endRatio * Math.PI * 2;
            let midRadian = midRatio * Math.PI * 2;
            if (startRatio === 0 && endRatio === 1) {
                isSelected = false;
            }
            k = typeof k !== 'undefined' ? k : 1 / 3;
            let offsetX = isSelected ? Math.cos(midRadian) * 0.1 : 0;
            let offsetY = isSelected ? Math.sin(midRadian) * 0.1 : 0;
            let hoverRate = isHovered ? 1.1 : 1;
            return {
                u: { min: -Math.PI, max: Math.PI * 3, step: Math.PI / 32 },
                v: { min: 0, max: Math.PI * 2, step: Math.PI / 20 },
                x: (u, v) => {
                    if (u < startRadian) {
                        return offsetX + Math.cos(startRadian) * (1 + Math.cos(v) * k) * hoverRate;
                    }
                    if (u > endRadian) {
                        return offsetX + Math.cos(endRadian) * (1 + Math.cos(v) * k) * hoverRate;
                    }
                    return offsetX + Math.cos(u) * (1 + Math.cos(v) * k) * hoverRate;
                },
                y: (u, v) => {
                    if (u < startRadian) {
                        return offsetY + Math.sin(startRadian) * (1 + Math.cos(v) * k) * hoverRate;
                    }
                    if (u > endRadian) {
                        return offsetY + Math.sin(endRadian) * (1 + Math.cos(v) * k) * hoverRate;
                    }
                    return offsetY + Math.sin(u) * (1 + Math.cos(v) * k) * hoverRate;
                },
                z: (u, v) => {
                    if (u < -Math.PI * 0.5) {
                        return Math.sin(u);
                    }
                    if (u > Math.PI * 2.5) {
                        return Math.sin(u);
                    }
                    return Math.sin(v) > 0 ? height : -1;
                },
            };
        },
        getPie3D(pieData, internalDiameterRatio, total) {
            let series = [];
            let sumValue = 0;
            let startValue = 0;
            let endValue = 0;
            let k = internalDiameterRatio !== undefined ? (1 - internalDiameterRatio) / (1 + internalDiameterRatio) : 1 / 3;
            for (let i = 0; i < pieData.length; i++) {
                sumValue += pieData[i].val;
                let seriesItem = {
                    name: pieData[i].name,
                    type: 'surface',
                    parametric: true,
                    wireframe: { show: false },
                    pieData: pieData[i],
                    pieStatus: { selected: false, hovered: false, k: k },
                    label: {
                        show: true,
                        formatter: params => {
                            let value = params.data.pieData.val; // 从 params.data.pieData 中获取值
                            return ((value / total) * 100).toFixed(2) + '%';
                        },
                        position: 'top',
                        color: '#fff',
                        fontSize: 14,
                        fontWeight: 'bold',
                        backgroundColor: 'rgba(0, 0, 0, 0.5)',
                        padding: [5, 10],
                        borderRadius: 4,
                    },
                };
                if (pieData[i].itemStyle) {
                    seriesItem.itemStyle = pieData[i].itemStyle;
                }
                series.push(seriesItem);
            }
            for (let i = 0; i < series.length; i++) {
                endValue = startValue + series[i].pieData.val;
                series[i].pieData.startRatio = startValue / sumValue;
                series[i].pieData.endRatio = endValue / sumValue;
                series[i].parametricEquation = this.getParametricEquation(
                    series[i].pieData.startRatio,
                    series[i].pieData.endRatio,
                    false,
                    false,
                    k,
                    series[i].pieData.val
                );
                startValue = endValue;
            }
            return series;
        },
        initChart() {
            const total = this.dataList.reduce((acc, item) => acc + item.val, 0);
            this.option = {
                legend: {
                    tooltip: { show: true },
                    data: this.dataList.map(item => item.name),
                    top: '5%',
                    left: '5%',
                    icon: 'circle',
                    textStyle: { color: '#fff', fontSize: 14 },
                },
                animation: true,
                labelLine: { show: true, lineStyle: { color: '#7BC0CB' } },
                label: { show: false },
                xAxis3D: { min: -1.5, max: 1.5 },
                yAxis3D: { min: -1.5, max: 1.5 },
                zAxis3D: { min: 0, max: 200 }, // Adjusted z-axis range
                grid3D: {
                    show: false,
                    boxHeight: 4,
                    bottom: '50%',
                    viewControl: { 
                        distance: 120, 
                        alpha: 25, 
                        beta: 60, 
                        autoRotate: false // Stop auto rotation
                    },
                },
                series: this.getPie3D(this.dataList, 0.8, total),
            };
            const chartDom = this.$refs.chart;
            if (chartDom) {
                const myChart = echarts.init(chartDom);
                myChart.setOption(this.option);
            }
        },
    },
};
</script>

<style scoped>
.chart-container {
    width: 100%;
    height: 100%;
}
</style>